﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ImageLibrary;
using EPiServer.Web;
using InitializationModule = EPiServer.Web.InitializationModule;

namespace EPiServer.Labs.MobilePack.Core.modules.MobilePack.Core.Manifest
{
    [InitializableModule]
    [ModuleDependency((typeof(InitializationModule)))]
    class ManifestInitialization : IInitializableModule
    {
        
        #region IInitializableModule Members

        /// <summary>
        /// Attach three events to resave the manifest file
        /// </summary>
        /// <param name="context"></param>
        public void Initialize(InitializationEngine context)
        {
            DataFactory.Instance.MovedPage += new PageEventHandler(InstancePageChanged);
            DataFactory.Instance.DeletedPage += new PageEventHandler(InstancePageChanged);
            DataFactory.Instance.PublishedPage += new PageEventHandler(InstancePageChanged);

            //if a code change is made we still need to resave the files to make sure it hasnt broken
            ManifestFactory.Instance.FindPagesAndAddThem();
            ManifestFactory.Instance.WriteManifestFile();
        }

        void InstancePageChanged(object sender, PageEventArgs e)
        {
            ManifestFactory.Instance.FindPagesAndAddThem();
            ManifestFactory.Instance.WriteManifestFile();
        }

        public void Preload(string[] parameters)
        {
            throw new NotImplementedException();
        }

        public void Uninitialize(InitializationEngine context)
        {
           DataFactory.Instance.MovedPage -= InstancePageChanged;
           DataFactory.Instance.DeletedPage -= InstancePageChanged;
           DataFactory.Instance.PublishedPage -= InstancePageChanged;
        }

        #endregion
    }
}
